import urllib
import urllib2
import cookielib
import json
import config


class BaseDriver(object):
    def __init__(self):
        self.opener = None
        self.cookiejar = None
        self.init_opener()

    def init_opener(self):
        self.cookiejar = cookielib.CookieJar()
        self.opener = urllib2.build_opener(
            urllib2.HTTPCookieProcessor(self.cookiejar)
        )

    def doRequest(self, action, values):
        data = urllib.urlencode(values)
        request = urllib2.Request(config.SERVER + action, data)
        response_str = self.opener.open(request).read()
        response = json.loads(response_str)
        return response